package com.yuandian.bpm.bi.modules.user.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuandian.bpm.bi.modules.user.entity.ConfAddresseeApp;
import com.yuandian.bpm.bi.modules.user.mapper.ConfAddresseeAppMapper;
import com.yuandian.bpm.bi.modules.user.service.ConfAddresseeAppService;
import com.yuandian.bpm.bi.modules.user.util.BatchDataResult;
import com.yuandian.bpm.bi.modules.user.util.ExcelService;
import com.yuandian.bpm.bi.modules.user.util.ExcelUtil;
import com.yuandian.bpm.bi.modules.user.vo.AddresseeAppExcelVo;
import com.yuandian.bpm.bi.modules.user.vo.ConfAddresseeAppVo;
import com.yuandian.bpm.common.core.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;

/**
 * 推送对象_应用-Service实现
 * @author zfsn
 * @date 2023-7-3
 */
@Service
public class ConfAddresseeAppServiceImpl extends ServiceImpl<ConfAddresseeAppMapper, ConfAddresseeApp> implements ConfAddresseeAppService {
	
	@Autowired
	private ConfAddresseeAppMapper confAddresseeAppMapper;

	/**
	 * 分页查询
	 *
	 * @param confAddresseeAppVo 筛选条件
	 * @return 通用返回对象
	 */
	@Override
	public Page<ConfAddresseeApp> listPage(ConfAddresseeAppVo confAddresseeAppVo) {
		return confAddresseeAppMapper.listPage(new Page<>(confAddresseeAppVo.getPageNum(), confAddresseeAppVo.getPageSize()), confAddresseeAppVo);
	}

	/**
	 * 通过主键查询数据
	 *
	 * @param id 主键集合
	 * @return 通用返回对象
	 */
	@Override
	public ConfAddresseeApp getOneById(Integer id) {
		return confAddresseeAppMapper.getOneById(id);
	}

	/**
	 * 新增数据
	 *
	 * @param confAddresseeApp 实例对象
	 * @return 通用返回对象
	 */
	@Override
	public R<String> saveData(ConfAddresseeApp confAddresseeApp) {
		confAddresseeAppMapper.saveData(confAddresseeApp);
		return R.ok();
	}

	/**
	 * 通过主键更新数据
	 *
	 * @param confAddresseeApp 实例对象
	 * @return 通用返回对象
	 */
	@Override
	public R<String> updateDataById(ConfAddresseeApp confAddresseeApp) {
		confAddresseeAppMapper.updateDataById(confAddresseeApp);
		return R.ok();
	}

	/**
	 * 通过主键删除数据
	 *
	 * @param id 主键
	 * @return 通用返回对象
	 */
	@Override
	public R<String> removeDataById(Integer id) {
		confAddresseeAppMapper.removeDataById(id);
		return R.ok();
	}

	/**
	 * 上传
	 *
	 * @param file file
	 * @return Result
	 */
	@Override
	public R upload(MultipartFile file) throws IOException {
		List<AddresseeAppExcelVo> successList = new ArrayList<>();
		List<AddresseeAppExcelVo> failList = new ArrayList<>();
		Map<String, List<AddresseeAppExcelVo>> resultMap = ExcelUtil.readAndSaveExcelData(file, 0, 2, AddresseeAppExcelVo.class, new ExcelService<AddresseeAppExcelVo>() {


			/**
			 * 保存导入的数据
			 *
			 * @param dataList
			 */
			@Override
			public void saveBatchExcelData(List<AddresseeAppExcelVo> dataList) {
				if (ObjectUtil.isNotEmpty(dataList)) {
					Set<ConfAddresseeApp> data = new HashSet<>();
					for (AddresseeAppExcelVo addresseeAppExcelVo : dataList) {
						ConfAddresseeApp confAddresseeApp = new ConfAddresseeApp();
						confAddresseeApp.setAppName(addresseeAppExcelVo.getAppName());
						confAddresseeApp.setAppType(addresseeAppExcelVo.getAppType());
						confAddresseeApp.setEnable(addresseeAppExcelVo.getEnable().equals("是") ? 1 : 0);
						confAddresseeApp.setWebHook(addresseeAppExcelVo.getWebHook());
						confAddresseeApp.setRemark(addresseeAppExcelVo.getRemark());
						data.add(confAddresseeApp);
					}
					ConfAddresseeAppServiceImpl.super.saveBatch(data);
				}
			}

			/**
			 * 入库前-批量校验数据
			 *
			 * @param dataList
			 * @return
			 */
			@Override
			public BatchDataResult<AddresseeAppExcelVo> checkBatchExcelData(List<AddresseeAppExcelVo> dataList) {
				//todo 未对数据进行校验
				successList.addAll(dataList);
				return new BatchDataResult<>(true, successList, failList);
			}

			@Override
			public boolean checkExcelData(AddresseeAppExcelVo data) {
				try {
					for (Field f : data.getClass().getDeclaredFields()) {
						f.setAccessible(true);
						if (f.get(data) == null) {
							return false;
						}
					}
				} catch (IllegalAccessException e) {
					return false;
				}
				return true;
			}


		});
		return R.ok(null,"导入成功的数据" + resultMap.get("result").size() + "条, 导入失败的数据有" + resultMap.get("fail").size() + "条");
	}
}